'''
Created on 2011-2-14

@author: wilbur.ma
'''

import os

from optparse import OptionParser

from pwilib.util.sourcerepo.SourceRepository import SourceRepository

def parse_cmd_args():
    usage = "usage: %prog [options] arg1 arg2"
    parser = OptionParser(usage=usage)
    
    parser.add_option('-i', '--init', dest='init', default=True,
                      help='Initialize the source repository.')
    parser.add_option('-h', '--help', 
                      help='Show help information')
    parser.add_option('--backup_sta', dest='backup_stable',  default=True, 
                      help='Backup stable codes.')
    parser.add_option('--backup_dev', dest='backup_stable',  default=False, 
                      help='Backup unstable codes.')
    parser.add_option('-u', '--upload', dest='upload', default='ftp', 
                      help='Upload repository by ftp.')
    parser.add_option('-d', '--download', dest='download', default='ftp',
                      help='Download repository by ftp.')
    
    options, args = parser.parse_args()
    if len(args) != 1:
        parser.error('Too many arguments to parse.')
    
    return options
    
    
def main():
    sr = SourceRepository(os.curdir())
    
    options = parse_cmd_args()
    if options.init:
        sr.create_dir_struct()
        sr.gen_config_file()
    if options.backup_sta is not None:
        sr.backup(options.backup_sta)
    elif options.upload:
        sr.ftp_connect()
        sr.ftp_upload()
    elif options.download:
        sr.ftp_connect()
        sr.ftp_download()
   
   
if __name__ == '__main__':
    main()